<template>
  <div class="table-area">
    <!-- 头部 -->
    <div class="filter">
      <el-form
        ref="filterForm"
        :model="filterForm"
        label-width="90px"
        label-position="left"
        size="small"
      >
        <div class="flexRBC">
          <el-form-item label="区域名称：" prop="name" size="small" style="marginBottom:0">
            <el-input v-model="filterForm.name" placeholder="请输入区域名称" />
            <el-button
              v-if="!filterCollapseActive"
              type="primary"
              round
              @keyup.enter.native="searchByKeyword"
              @click="searchByKeyword"
            >查 询</el-button>
          </el-form-item>
          <el-button type="primary" size="small" round @click="edit('add')">新增区域</el-button>
        </div>
        <!-- 筛选 -->
        <!-- <div class="app-main-filter-collapse-box">
          <el-collapse @change="filterCollapseChange">
            <el-collapse-item title="按条件筛选" name="1" ref="filterCollapse">
              <el-form-item label="所属区域：" prop="areaId">
                <el-select v-model="filterForm.areaId" filterable placeholder="请选择">
                  <el-option
                    v-for="item in areaIdOptions"
                    :key="item.value"
                    :label="item.name"
                    :value="item.id"
                  ></el-option>
                </el-select>
              </el-form-item>
              <el-form-item class="filter-btn">
                <el-button type="primary" round @click="submitForm('filterForm')">查 询</el-button>
                <el-button round @click="resetForm('filterForm')">重 置</el-button>
              </el-form-item>
            </el-collapse-item>
          </el-collapse>
        </div>-->
      </el-form>
    </div>

    <div class="content">
      <!-- 表格内容 -->
      <div class="list-table">
        <el-table
          v-loading="tableLoading"
          :data="list"
          border
          stripe
          style="width: 100%"
          empty-text="暂未设置桌台"
        >
          <el-table-column prop="name" label="区域名称" fixed />
          <el-table-column prop="tableCount" label="桌台数" />
          <el-table-column label="操作" width="100">
            <template slot-scope="scope">
              <el-button type="text" size="mini" @click="edit('edit',scope.row)">修改</el-button>
              <el-button type="text" size="mini" @click="del(scope.row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </div>
    <!-- 编辑 -->
    <dialog-edit-area ref="editArea" />
  </div>
</template>

<script>
import DialogEditArea from './DialogEditArea'
import { delTableArea, getTableAreasList } from '../../../api/table'
export default {
  components: {
    DialogEditArea
  },
  props: {
    shopId: {
      type: Number,
      required: true
    }
  },
  data() {
    return {
      // 配置
      // 状态
      tableLoading: false, // 表格loading
      searchLoading: false, // 表格loading
      filterCollapseActive: false, // 筛选是否打开
      // 数据
      list: [], // 表格数据
      // 筛选数据
      filterForm: {
        name: ''
      }
    }
  },
  watch: {
    shopId(val) {
      this.initData()
    }
  },
  mounted() {
    this.initData()
  },
  methods: {
    /**
       * 获取数据
       */
    // 获取门店数据
    initData() {
      this.filterForm.shopId = this.shopId
      const params = Object.assign({}, this.filterForm)
      this.tableLoading = true
      getTableAreasList(params)
        .then(res => {
          this.list = res.data
        })
        .finally(() => {
          this.tableLoading = false
          this.searchLoading = false
        })
    },

    /**
       * 按钮组
       */
    // 点击搜索
    searchByKeyword() {
      this.searchLoading = true
      this.initData(1)
    },

    // 点击新增 编辑
    edit(type, row = '') {
      this.$refs.editArea.open(type, this.shopId, row).then(res => {
        this.initData()
      })
    },

    // 删除
    del(row) {
      this.$confirm(`你确认要删除区域：${row.name}, 是否继续?`, '删除提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
        center: true,
        roundButton: true
      })
        .then(() => {
          this.tableLoading = true
          delTableArea(row)
            .then(res => {
              this.$notify({
                title: '成功',
                message: '删除区域成功',
                type: 'success',
                duration: 2000,
                offset: 40
              })
              this.initData()
            })
            .catch(err => {})
            .finally(() => {
              this.tableLoading = false
            })
        })
        .catch(() => {})
    },

    // 重置
    resetForm(formName) {
      this.$refs[formName].resetFields()
    },

    /**
       * 状态组
       */
    // 筛选
    filterCollapseChange(val) {
      if (val.length === 0) {
        this.filterCollapseActive = false
      } else {
        this.filterCollapseActive = true
      }
    }
  }
}
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
  .table-area {
    .el-input {
      width: 300px;
      margin-right: 10px;
    }
    // 单选换行
    .el-radio + .el-radio {
      margin-left: 0;
    }
    .el-radio {
      margin-right: 10px;
      line-height: 32px;
    }
    .el-select {
      width: 300px;
    }

    /* 表格 */
    .content {
      margin-top: 10px;
    }
  }
</style>
